import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import './router/permission'
import axios from 'axios'
// import '@/assets/main.css'
import SvgIcon from './components/SvgIcon.vue'
import Cookies from 'js-cookie';

// 创建多个 axios 实例
const apiAxios = axios.create({
  baseURL: import.meta.env.MODE === 'development' ? '/api' : 'http://8.137.173.171:9090',
  timeout: 10000,
  withCredentials: true
})

const agentAxios = axios.create({
  baseURL: import.meta.env.MODE === 'development' ? '/agent' : 'http://8.137.173.171:8088',
  timeout: 10000,
  withCredentials: true
})

// 为 apiAxios 添加请求拦截器
apiAxios.interceptors.request.use(
  config => {
    // 从 sessionStorage 中获取 token
    const token = sessionStorage.getItem('token')
    console.log('1111token',token);
    if (token) {
      // 将 token 添加到请求头
      config.headers['Content-Type'] = 'application/json'
      config.headers['token'] = token
    }

    // 如果是 POST 请求，确保数据是 JSON 格式
    if (config.method === 'post' && config.data) {
      config.data = JSON.stringify(config.data)
    }
    
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 为 agentAxios 添加请求拦截器
agentAxios.interceptors.request.use(
  config => {
    // 设置通用请求头
    config.headers['Content-Type'] = 'application/json'
    config.headers['Accept'] = 'application/json'
    
    // 设置请求体格式
    config.transformRequest = [(data) => {
      return JSON.stringify(data)
    }]
    
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 为 apiAxios 添加响应拦截器
apiAxios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // token 过期或无效，清除 token 并跳转到登录页
          Cookies.remove('token')
          router.push('/login')
          break
        case 403:
          // 权限不足
          console.error('权限不足')
          break
        default:
          console.error('请求错误:', error.response.data)
      }
    }
    return Promise.reject(error)
  }
)

// 为 agentAxios 添加响应拦截器
agentAxios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 403:
          console.error('Agent 服务权限不足')
          break
        case 404:
          console.error('Agent 服务接口不存在')
          break
        case 500:
          console.error('Agent 服务内部错误')
          break
        default:
          console.error('Agent 服务请求错误:', error.response.data)
      }
    }
    return Promise.reject(error)
  }
)

// 将 axios 实例挂载到全局
const app = createApp(App)
app.config.globalProperties.$apiAxios = apiAxios as any
app.config.globalProperties.$agentAxios = agentAxios as any

app.use(createPinia())
app.use(ElementPlus)
app.use(router)

// 注册 SvgIcon 组件
app.component('svg-icon', SvgIcon)

app.mount('#app')


// 登录后如果在登录页，自动跳转首页
const token = sessionStorage.getItem('token') || localStorage.getItem('token');
if (token && (window.location.pathname === '/login' || window.location.pathname === '/login/')) {
  window.location.replace('/home');
}